#include "configapi.hpp"
///>通过文件配置库操作,键值保存
using namespace configraion;

void FactoryConfigGet(void)
{
	keyvalue key(FACTORYCONFIG_DISK, CONFIG_PRODUCTINFOR_NAME);
	key.KeyDemo = mKeyDemoProductInfor;
	key.KeyLen = sizeof(mKeyDemoProductInfor) / sizeof(keyvalue::KeyDemoTPDF);
	if(!key.Open(&mTarget.tProductInfor))
	{
		mTarget.tProductInfor.ucManufactoryId = ProtectInfor_ManufactoryId_Attribute.tVariableDataLimit.iLower;
		mTarget.tProductInfor.ucClass = ProtectInfor_Class_Attribute.tVariableDataLimit.iLower;
		mTarget.tProductInfor.ucSerial = ProtectInfor_Serial_Attribute.tVariableDataLimit.iLower;
		mTarget.tProductInfor.ucPriSerial = ProtectInfor_PriSerial_Attribute.tVariableDataLimit.iLower;
		mTarget.tProductInfor.ucHardwareSerial[0] = (ProtectInfor_HardwareSerial_Attribute.tVariableDataLimit.iLower >> 8) & 0x00ff;
		mTarget.tProductInfor.ucHardwareSerial[1] = (ProtectInfor_HardwareSerial_Attribute.tVariableDataLimit.iLower) & 0x00ff;
		mTarget.tProductInfor.usCurrent = 1600;				///<ProtectInfor_Current_Attribute.tVariableDataLimit.iLower;
		mTarget.tProductInfor.usStructCurrent = 1600;	///ProtectInfor_StructCurrent_Attribute.tVariableDataLimit.iLower;
		mTarget.tProductInfor.usAttribute = 0x34;			///<0;
	}
}

void FactoryConfigSet(void)
{
	keyvalue key(FACTORYCONFIG_DISK, CONFIG_PRODUCTINFOR_NAME);
	key.KeyDemo = mKeyDemoProductInfor;
	key.KeyLen = sizeof(mKeyDemoProductInfor) / sizeof(keyvalue::KeyDemoTPDF);
	key.Save(&mTarget.tProductInfor);
}

void CalibrationGet(CalibrationTPDF *tCalibration)
{
	if(!file::Read(FACTORYCONFIG_DISK, CONFIG_METERCALI_NAME, (uint8_t *)tCalibration, sizeof(CalibrationTPDF)))
	{
		tCalibration->uiIaL = CONFIG_CALIBRATION_LOWER;
		tCalibration->uiIbL = CONFIG_CALIBRATION_LOWER;
		tCalibration->uiIcL = CONFIG_CALIBRATION_LOWER;
		tCalibration->uiIaH = CONFIG_CALIBRATION_UPPER;
		tCalibration->uiIbH = CONFIG_CALIBRATION_UPPER;
		tCalibration->uiIcH = CONFIG_CALIBRATION_UPPER;
		tCalibration->uiIn = CONFIG_CALIBRATION_UPPER;
		tCalibration->uiIg = CONFIG_CALIBRATION_UPPER;
		tCalibration->uiVref = 2047;
	}
}

void CalibrationSet(CalibrationTPDF *tCalibration)
{
	file::Write(FACTORYCONFIG_DISK, CONFIG_METERCALI_NAME, (uint8_t *)tCalibration, sizeof(CalibrationTPDF));
}

void ComConfigGet(void)
{
	keyvalue key(CONFIG_DISK, CONFIG_COMCONFIG_NAME);
	key.KeyDemo = mKeyDemoComConfig;
	key.KeyLen = sizeof(mKeyDemoComConfig) / sizeof(keyvalue::KeyDemoTPDF);
	if(!key.Open(&mTarget.tComConfig))
	{
		mTarget.tComConfig.usAddr = ComConfig_Addr_Attribute.tVariableDataLimit.iLower;
		mTarget.tComConfig.usBaud = ComConfig_Baud_Attribute.tVariableDataLimit.iLower;
	}
}

void ComConfigSet(void)
{
	keyvalue key(CONFIG_DISK, CONFIG_COMCONFIG_NAME);
	key.KeyDemo = mKeyDemoComConfig;
	key.KeyLen = sizeof(mKeyDemoComConfig) / sizeof(keyvalue::KeyDemoTPDF);
	key.Save(&mTarget.tComConfig);
}

void SecurityConfigGet(void)
{
	keyvalue key(CONFIG_DISK, CONFIG_SECURITY_NAME);
	key.KeyDemo = mKeyDemoSecurity;
	key.KeyLen = sizeof(mKeyDemoSecurity) / sizeof(keyvalue::KeyDemoTPDF);
	if(!key.Open(&mTarget.tSecurityConfig))
	{
		mTarget.tSecurityConfig.usLanguage = Security_Language_Attribute.tVariableDataLimit.iLower;
		mTarget.tSecurityConfig.usPasswordEnable = Security_PasswordEnable_Attribute.tVariableDataLimit.iLower;
		mTarget.tSecurityConfig.usNetChangeEnable = Security_NetChangeEnable_Attribute.tVariableDataLimit.iLower;
		mTarget.tSecurityConfig.usNetControlEnable = Security_NetControlEnable_Attribute.tVariableDataLimit.iLower;
		memset(mTarget.tSecurityConfig.ucPassword, '0', 8);
	}
}

void SecurityConfigSet(void)
{
	keyvalue key(CONFIG_DISK, CONFIG_SECURITY_NAME);
	key.KeyDemo = mKeyDemoSecurity;
	key.KeyLen = sizeof(mKeyDemoSecurity) / sizeof(keyvalue::KeyDemoTPDF);
	key.Save(&mTarget.tSecurityConfig);
}

void CustomConfigDefault(bool bResetAll)
{
	LcdCustomConfigTPDF *lcdptr = &mTarget.tLcdCustomConfig;
	int32_t max,min;
	
	if(mTarget.tProductInfor.usAttribute & CONFIG_ATTRIBUTE_FLAG_MOTOR)
	{
		if(bResetAll)
		{
			lcdptr->usLongDelayCurrent 		= LcdCustomConfig_LongDelayCurrent_Attribute.tVariableDataLimit.iUpper * mTarget.tProductInfor.usCurrent / 100;
			lcdptr->usLongDelayTime 			= LcdCustomConfig_Motor_LongDelayTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usShortDelayCurrent 	= lcdptr->usLongDelayCurrent * 4;
			lcdptr->usShortDelayTime 			= LcdCustomConfig_Motor_ShortDelayTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usEarthTime 					= LcdCustomConfig_EarthTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usNCurrent 						= 10;
			lcdptr->usEarthCurrent 				= mTarget.tProductInfor.usCurrent;
			lcdptr->usUnbalanceRated 			= LcdCustomConfig_UnbalanceRated_Attribute.tVariableDataLimit.iUpper;
			lcdptr->usUnbalanceTime 			= LcdCustomConfig_UnbalanceTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usBlockCurrent 				= mTarget.tProductInfor.usCurrent * 10;
			lcdptr->usBlockTime 					= LcdCustomConfig_BlockTime_Attribute.tVariableDataLimit.iUpper;
			lcdptr->usLongDelayEnable 		= 1;
			lcdptr->usShortDelayEnable		= 1;
			lcdptr->usShortCircuitEnable 	= 1;
			lcdptr->usNEnable 						= 0;
			lcdptr->usEarthEnable 				= 0;
			lcdptr->usUnbalanceEnable 		= 0;
			lcdptr->usBlockEnable 				= 0;
			lcdptr->usFrequnce 						= LcdCustomConfig_Frequnce_Attribute.tVariableDataLimit.iLower;
		}
		if(lcdptr->usLongDelayEnable)
		{
			max = LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iUpper;
			min = LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iLower;
			if(lcdptr->usShortDelayCurrent > (max * lcdptr->usLongDelayCurrent / 100) || 
				 lcdptr->usShortDelayCurrent < (min * lcdptr->usLongDelayCurrent / 100))
			{
				lcdptr->usShortDelayCurrent = min * lcdptr->usLongDelayCurrent / 100;
			}
			max = LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iUpper;
			min = LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iLower;
			if(lcdptr->usBlockCurrent > (max * lcdptr->usLongDelayCurrent / 100) || 
				 lcdptr->usBlockCurrent < (min * lcdptr->usLongDelayCurrent / 100))
			{
				lcdptr->usBlockCurrent = min * lcdptr->usLongDelayCurrent / 100;
			}
		}
		else
		{
			max = LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iUpper;
			min = LcdCustomConfig_Motor_ShortDelayCurrent_Attribute.tVariableDataLimit.iLower;
			if(lcdptr->usShortDelayCurrent > (max * mTarget.tProductInfor.usCurrent / 100) || 
				 lcdptr->usShortDelayCurrent < (min * mTarget.tProductInfor.usCurrent / 100))
			{
				lcdptr->usShortDelayCurrent = min * mTarget.tProductInfor.usCurrent / 100;
			}
			max = LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iUpper;
			min = LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iLower;
			if(lcdptr->usBlockCurrent > (max * mTarget.tProductInfor.usCurrent / 100) || 
				 lcdptr->usBlockCurrent < (min * mTarget.tProductInfor.usCurrent / 100))
			{
				lcdptr->usBlockCurrent = min * mTarget.tProductInfor.usCurrent / 100;
			}
		}
		max = LcdCustomConfig_Motor_ShortCircuitCurrent_Attribute.tVariableDataLimit.iUpper;
		min = LcdCustomConfig_Motor_ShortCircuitCurrent_Attribute.tVariableDataLimit.iLower;
		if(lcdptr->usShortCircuitCurrent	> (max * mTarget.tProductInfor.usCurrent / 100) ||
			 lcdptr->usShortCircuitCurrent	< (min * mTarget.tProductInfor.usCurrent / 100))
		{
			lcdptr->usShortCircuitCurrent	= min * mTarget.tProductInfor.usCurrent / 100;
		}
		max = LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iUpper;
		min = LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iLower;
		if(lcdptr->usEarthCurrent > (max * mTarget.tProductInfor.usCurrent / 100) ||
			 lcdptr->usEarthCurrent < (min * mTarget.tProductInfor.usCurrent / 100))
		{
			lcdptr->usEarthCurrent = min * mTarget.tProductInfor.usCurrent / 100;
		}
	}
	else
	{
		if(bResetAll)
		{
			lcdptr->usLongDelayCurrent 		= LcdCustomConfig_LongDelayCurrent_Attribute.tVariableDataLimit.iUpper * mTarget.tProductInfor.usCurrent / 100;
			lcdptr->usLongDelayTime 			= LcdCustomConfig_Distrib_LongDelayTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usShortDelayCurrent 	= lcdptr->usLongDelayCurrent * 3;
			lcdptr->usShortDelayTime 			= LcdCustomConfig_Distrib_ShortDelayTime_Attribute.tVariableDataLimit.iUpper;
			lcdptr->usShortCircuitCurrent	= mTarget.tProductInfor.usCurrent * 5;
			lcdptr->usEarthTime 					= LcdCustomConfig_EarthTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usNCurrent 						= 10;
			lcdptr->usEarthCurrent 				= mTarget.tProductInfor.usCurrent;
			lcdptr->usUnbalanceRated 			= LcdCustomConfig_UnbalanceRated_Attribute.tVariableDataLimit.iUpper;
			lcdptr->usUnbalanceTime 			= LcdCustomConfig_UnbalanceTime_Attribute.tVariableDataLimit.iLower;
			lcdptr->usBlockCurrent 				= mTarget.tProductInfor.usCurrent * 10;
			lcdptr->usBlockTime 					= LcdCustomConfig_BlockTime_Attribute.tVariableDataLimit.iUpper;
			lcdptr->usLongDelayEnable 		= 1;
			lcdptr->usShortDelayEnable		= 1;
			lcdptr->usShortCircuitEnable 	= 1;
			lcdptr->usNEnable 						= 1;
			lcdptr->usEarthEnable 				= 0;
			lcdptr->usUnbalanceEnable 		= 0;
			lcdptr->usBlockEnable 				= 0;
			lcdptr->usFrequnce 						= LcdCustomConfig_Frequnce_Attribute.tVariableDataLimit.iLower;
		}
		max = LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute.tVariableDataLimit.iUpper;
		min = LcdCustomConfig_Distrib_ShortDelayCurrent_Attribute.tVariableDataLimit.iLower;
		if(lcdptr->usShortDelayCurrent > (max * lcdptr->usLongDelayCurrent / 100) || 
			 lcdptr->usShortDelayCurrent < (min * lcdptr->usLongDelayCurrent / 100))
		{
			lcdptr->usShortDelayCurrent = min * lcdptr->usLongDelayCurrent / 100;
		}
		max = LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iUpper;
		min = LcdCustomConfig_BlockCurrent_Attribute.tVariableDataLimit.iLower;
		if(lcdptr->usBlockCurrent > (max * lcdptr->usLongDelayCurrent / 100) || 
			 lcdptr->usBlockCurrent < (min * lcdptr->usLongDelayCurrent / 100))
		{
			lcdptr->usBlockCurrent = min * lcdptr->usLongDelayCurrent / 100;
		}
		max = LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute.tVariableDataLimit.iUpper;
		min = LcdCustomConfig_Distrib_ShortCircuitCurrent_Attribute.tVariableDataLimit.iLower;
		if(lcdptr->usShortCircuitCurrent	> (max * mTarget.tProductInfor.usCurrent / 100) ||
			 lcdptr->usShortCircuitCurrent	< (min * mTarget.tProductInfor.usCurrent / 100))
		{
			lcdptr->usShortCircuitCurrent	= min * mTarget.tProductInfor.usCurrent / 100;
		}
		max = LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iUpper;
		min = LcdCustomConfig_EarthCurrent_Attribute.tVariableDataLimit.iLower;
		if(lcdptr->usEarthCurrent > (max * mTarget.tProductInfor.usCurrent / 100) ||
			 lcdptr->usEarthCurrent < (min * mTarget.tProductInfor.usCurrent / 100))
		{
			lcdptr->usEarthCurrent = min * mTarget.tProductInfor.usCurrent / 100;
		}
	}
}

void CustomConfigGet(void)
{
	keyvalue key(CONFIG_DISK, CONFIG_LCDCUSTOMCONFIG_NAME);
	key.KeyDemo = mKeyDemoLcdCustomConfig;
	key.KeyLen = sizeof(mKeyDemoLcdCustomConfig) / sizeof(keyvalue::KeyDemoTPDF);
	if(!key.Open(&mTarget.tLcdCustomConfig))
	{
		CustomConfigDefault(true);
	}
}

void CustomConfigSet(void)
{
	keyvalue key(CONFIG_DISK, CONFIG_LCDCUSTOMCONFIG_NAME);
	key.KeyDemo = mKeyDemoLcdCustomConfig;
	key.KeyLen = sizeof(mKeyDemoLcdCustomConfig) / sizeof(keyvalue::KeyDemoTPDF);
	key.Save(&mTarget.tLcdCustomConfig);
}
